package mobile.game.core.gen.proto;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class ProtoPatcherWarning {

	private final String IPath;

	private List<File> patcherFiles = new ArrayList<File>();
	
	private final List<String> ignoreWords;

	private final String PATCHER_STRING = "public final class";
	private final String SUPPRESSWARNINGS = "@SuppressWarnings(\"all\")";

	/**
	 * 构造函数
	 */
	public ProtoPatcherWarning(String IPath, List<String> ignoreWords) {
		this.IPath = IPath;
		this.ignoreWords = ignoreWords;
	}

	/**
	 * 获取所有的msg文件
	 */
	private void collectMsgFiles() {
		File dir = new File(IPath);
		for (File f : dir.listFiles()) {
			if (f.isDirectory()) {
				continue;
			}

			if (f.getName().endsWith(".java")) {
				patcherFiles.add(f);
			}
		}
	}

	private void patcherFile(File inf) throws IOException {
		System.out.println("patcherFile:" + inf.toString());

		File sourceNew = new File(inf.toString() + ".tmp");
		sourceNew.createNewFile();

		InputStreamReader reader = null;
		BufferedReader bufferedReader = null;
		OutputStreamWriter writer = null;
		try {

			reader = new InputStreamReader(new FileInputStream(inf), "UTF-8");
			bufferedReader = new BufferedReader(reader);
			writer = new OutputStreamWriter(new FileOutputStream(sourceNew), "UTF-8");

			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				if (line.contains(SUPPRESSWARNINGS)) {
					writer.write(line + "\n");
					break;
				} else if (line.contains(PATCHER_STRING)) {
					writer.write(SUPPRESSWARNINGS + "\n");
					writer.write(line + "\n");
					break;
				}
				
				writer.write(line + "\n");
			}

			// 拷贝余下内容
			while ((line = bufferedReader.readLine()) != null) {
				writer.write(line + "\n");
			}
		} finally {
			reader.close();
			bufferedReader.close();
			writer.close();
		}

		inf.delete();
		sourceNew.renameTo(inf);
	}

	public void Gen() {
		// 收集message文件
		this.collectMsgFiles();

		// 忽略
		patcherFiles.removeIf(s -> {
			for (String ignoreWord : ignoreWords) {
				if (s.getName().contains(ignoreWord)) {
					return true;
				}
			}
			
			return false;
		});
		
		for (File file : patcherFiles) {
			for (String ignoreWord : ignoreWords) {
				if (file.getName().contains(ignoreWord)) {
					
				}
			}
			
		}
		
		// 处理文件
		for (File file : patcherFiles) {
			try {
				this.patcherFile(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		if (args.length < 1) {
			return;
		}
		
		List<String> ignoreWords = new ArrayList<String>();

		String IPath = args[0];
		for (int i=1; i<args.length; ++i) {
			ignoreWords.add(args[i]);
		}

		ProtoPatcherWarning protoPatcherWarning = new ProtoPatcherWarning(IPath, ignoreWords);
		protoPatcherWarning.Gen();
	}

}
